package com.example.figurehecheng.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.figurehecheng.common.context.BaseContext;
import com.example.figurehecheng.common.result.PageResult;
import com.example.figurehecheng.domain.dto.SubscribePageDTO;
import com.example.figurehecheng.domain.entity.Subscribe;
import com.example.figurehecheng.mapper.SubscribeMapper;
import com.example.figurehecheng.service.ISubscribeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 宇科工作室
 * @since 2024-10-18
 */
@Service
@RequiredArgsConstructor
public class SubscribeServiceImpl extends ServiceImpl<SubscribeMapper, Subscribe> implements ISubscribeService {

    private final SubscribeMapper subscribeMapper;

    /**
     * 查询预约
     * @param subscribePageDTO
     * @return
     */
    @Override
    public PageResult selectPageSubscribe(SubscribePageDTO subscribePageDTO) {
        Page<Subscribe> subscribePage = new Page<>(subscribePageDTO.getPageNum(),subscribePageDTO.getPageSize());
        LambdaQueryWrapper<Subscribe> subscribeLambdaQueryWrapper = new LambdaQueryWrapper<Subscribe>()
                .eq(Subscribe::getShopId, BaseContext.getCurrentId())
                .eq(StrUtil.isNotBlank(subscribePageDTO.getOrderNumber()), Subscribe::getOrderNumber, subscribePageDTO.getOrderNumber())
                .ge(subscribePageDTO.getBeganTime() != null, Subscribe::getBeginTime, subscribePageDTO.getBeganTime())
                .le(subscribePageDTO.getEndTime() != null, Subscribe::getEndTime, subscribePageDTO.getEndTime())
                .eq(subscribePageDTO.getState() != null, Subscribe::getState, subscribePageDTO.getState())
                .orderByDesc(Subscribe::getBeginTime)
                .orderByDesc(Subscribe::getEndTime);
        Page<Subscribe> selectPage = subscribeMapper.selectPage(subscribePage, subscribeLambdaQueryWrapper);
        long total = selectPage.getTotal();
        List<Subscribe> records = selectPage.getRecords();
        return new PageResult(total, records);
    }

    /**
     * 修改预约状态
     * @param id
     * @param state
     * @return
     */
    @Override
    public void updateState(Integer id, Integer state) {
        Subscribe subscribe = new Subscribe();
        subscribe.setId(id);
        subscribe.setState(state);
        subscribeMapper.updateById(subscribe);
    }
}
